package com.mask.sca.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mask.sca.system.dto.MenuQuery;
import com.mask.sca.system.entity.Menu;
import com.mask.sca.system.mapper.MenuMapper;
import com.mask.sca.system.service.MenuService;
import com.mask.sca.system.util.MenuTreeUtil;
import com.mask.sca.system.vo.MenuTreeVO;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements MenuService {
    @Override
    public Page<Menu> pageMenus(MenuQuery query) {
        Page<Menu> page = new Page<>(query.getPageNum(), query.getPageSize());
        LambdaQueryWrapper<Menu> wrapper = new LambdaQueryWrapper<>();
        if (query.getName() != null && !query.getName().isEmpty()) {
            wrapper.like(Menu::getName, query.getName());
        }
        if (query.getType() != null && !query.getType().isEmpty()) {
            wrapper.eq(Menu::getType, query.getType());
        }
        if (query.getEnabled() != null) {
            wrapper.eq(Menu::getEnabled, query.getEnabled());
        }
        wrapper.orderByAsc(Menu::getOrderNum);
        return this.page(page, wrapper);
    }

    @Override
    public boolean removeBatch(List<Long> ids) {
        return this.removeByIds(ids);
    }

    @Override
    public List<MenuTreeVO> tree() {
        List<Menu> allMenus = this.list();
        return MenuTreeUtil.buildTree(allMenus);
    }
} 